package com.kuke.kukecommon.entity;

import com.kuke.kukecommon.enums.ResultEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("rest请求的返回模型，所有rest正常都返回该类的对象")
public class Result<T> {

    /**
     * 返回码
     */
    @ApiModelProperty(value = "结果码", dataType = "String", required = true)
    private String code;
    /**
     * 返回消息
     */
    @ApiModelProperty(value = "结果描述", dataType = "String", required = true)
    private String msg;

    @ApiModelProperty(value = "请求结果生成时间(单位秒)", dataType = "String", required = true)
    private String time;

    /**
     * 返回数据
     */
    @ApiModelProperty("响应数据对象")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

//1.成功处理-------------------------------------------------------------------------------------------------
    /**
     * 1.1全参数返回，不建议直接使用此方法（全参数）
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Result success(String code, String msg, Object data){
        Result result = new Result();

        //赋值code,处理null和空
        if(null == code || "".equals(code)){
            result.setCode(ResultEnum.SUCCESS.getCode());
        }else {
            result.setCode(code);
        }

        //赋值msg,处理null和空
        if(null == msg || "".equals(msg)){
            result.setMsg(ResultEnum.SUCCESS.getMsg());
        }else {
            result.setMsg(msg);
        }

        result.setTime(String.valueOf(System.currentTimeMillis()/1000));

        if (null == data){
            result.setData(new Object());
        }else {
            result.setData(data);
        }

        return result;
    }


    /**
     * 1.2仅传返回对象（1个参数）
     * @param data
     * @return
     */
    public static Result success(Object data){
        return success(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),data);
    }

    /**
     * 1.3仅返回成功状态（0参数）
     * @return
     */
    public static Result success(){
        return success(new Object());
    }


    /**
     * 1.4返回枚举和数据（2个参数）
     * @param resultEnum
     * @param data
     * @return
     */
    public static Result success(ResultEnum resultEnum,Object data){
        return success(resultEnum.getCode(),resultEnum.getMsg(),data);
    }




//2.操作失败流程--------------------------------------------------------------------------------------------


    /**
     * 2.1操作失败流程(全参数)
     * @param code
     * @param msg
     * @return
     */
    public static Result error(String code, String msg,Object data){
        Result result = new Result();
        //赋值code,处理null和空
        if(null == code || "".equals(code)){
            result.setCode(ResultEnum.SYSTEM_ERROR.getCode());
        }else {
            result.setCode(code);
        }

        //赋值msg,处理null和空
        if(null == msg || "".equals(msg)){
            result.setMsg(ResultEnum.SYSTEM_ERROR.getMsg());
        }else {
            result.setMsg(msg);
        }

        result.setTime(String.valueOf(System.currentTimeMillis()/1000));

        if (null == data){
            result.setData(new Object());
        }else {
            result.setData(data);
        }
        return result;
    }


    /**
     * 2.2重载，操作失败的方法（2参数 因为操作失败一般都不需要返回数据内容）
     * @param code
     * @param msg
     * @return
     */
    public static Result error(String code, String msg){
        return error(code, msg, null);
    }

    /**
     * 2.3枚举类型，返回对象
     * @param resultEnum
     * @return
     */
    public static Result error(ResultEnum resultEnum){
        return error(resultEnum.getCode(),resultEnum.getMsg());
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
